package reflect;

import org.junit.Test;

/**
 * @description: 静态代理
 * @author: apple
 * @date:  2021/12/30 11:37 下午
 * @version 1.0
 **/
public class StaticProxyTest {

    @Test
    public void test1() {

        // 创建一个代理，传入被代理类的实例
        ProxyClothFactory factory = new ProxyClothFactory(new NikeClothFactory());
        factory.productCloth();

        ProxyClothFactory2 factory2 = new ProxyClothFactory2(new NikeClothFactory());
        factory2.productCloth();
    }
}

// 制造衣服的接口
interface ClothFactory {

    void productCloth();
}
// 实现接口的类
class NikeClothFactory implements ClothFactory {

    @Override
    public void productCloth() {
        System.out.println("制造Nike");
    }
}

// 代理类，实现同一个接口
class ProxyClothFactory implements ClothFactory {

    // 用被代理类实例
    private ClothFactory clothFactory;

    public ProxyClothFactory(ClothFactory clothFactory) {
        this.clothFactory = clothFactory;
    }

    @Override
    public void productCloth() {
        System.out.println("工厂：" + clothFactory.getClass());
        System.out.println("开始..");
        clothFactory.productCloth();
        System.out.println("结束..");
    }
}


// 代理类未能实现接口
class ProxyClothFactory2 {

    // 用被代理类实例
    private ClothFactory clothFactory;

    public ProxyClothFactory2(ClothFactory clothFactory) {
        this.clothFactory = clothFactory;
    }

    public void productCloth() {
        System.out.println("工厂：" + clothFactory.getClass());
        System.out.println("开始..");
        clothFactory.productCloth();
        System.out.println("结束..");
    }
}